/*
    Yig - A network music instrument for constructing feedback matrices synchronized with OSC using OSCthulhu
    Chad McKinney, 2011

    Licensed under GNU GPL 3.0
    See COPYING for licensing details
*/

// System includes
#include <QApplication>
#include <QSemaphore>
#include <QDebug>
#include <QPlastiqueStyle>

// Local includes
#include "yig.h"
#include "sc++/sclang.h"
#include "gui/yigloginmenu.h"

// argv[1] = Oscthulhu server password, necessary to delete objects from the server
// argv[2] = User Name
// argv[3] = User password
// argv[4] = OSCthulhuClient port

int main(int argc, char *argv[])
{
    //QApplication::setGraphicsSystem("opengl");
    // Qt Application
    QApplication a(argc, argv);
    a.setStyle(new QPlastiqueStyle);
    // This makes sure we clean up everything before exiting
    QSemaphore* wait4CleanupDone = new QSemaphore(1);

    //bool online = true;
    //QString userName = "octopian";

    LogInInformation login;

    //YigLogInMenu logIn(&login);

    //qDebug() << login.userName + " Online: " << login.online;

    //oscthulhuClient("Oscthulhu Client", "127.0.0.1", oscthulhuClientPort, 7000)
    // scSynthClient("SCSynth Client", "127.0.0.1", serverPort, clientPort)
    int localPort = 32244;
    YigClient yigClient("127.0.0.1", localPort);

    // SuperCollider client
    SCLang scLang(&yigClient, a.applicationDirPath(), localPort, 67110);
    //scLang.bootServer();

    // Main Yig class
    // argv[4] = OSCthulhu Client port
    // argv[2] = user name;
    Yig yig(login.online, &yigClient, &scLang, a.applicationDirPath(), wait4CleanupDone);
    yigClient.setYig(&yig);

    // Connect the core application quit signal to the Yig and SCLang handle quit slot
    QObject::connect(&a, SIGNAL(aboutToQuit()), &yig, SLOT(handleQuit()));

    // Start networking
    //yigClient.start();

    // Hold the exec until we've acquired the semaphore
    int r = a.exec();

    wait4CleanupDone->acquire();
    return r;
}
